using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WowDotNetAPI.Extensions;
using WowDotNetAPI.Models;
using WowDotNetAPI.Utilities;

namespace WowDotNetAPI.Explorers.Test
{
    [TestClass]
    public class DataTest : SilverlightTest
    {
        [TestMethod]
        [Asynchronous]
        public void Get_Character_Races_Data()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            IEnumerable<CharacterRaceInfo> races = null;
            WowExplorer.GetCharacterRaces(cb =>
                    {
                        races = cb;
                        done = true;
                    });


            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsTrue(races.Count() == 12));
            EnqueueCallback(() => Assert.IsTrue(races.Any(r => r.Name == "Human" || r.Name == "Night Elf")));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Character_Classes_Data()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            IEnumerable<CharacterClassInfo> classes = null;
            WowExplorer.GetCharacterClasses(cb =>
            {
                classes = cb;
                done = true;
            });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsTrue(classes.Count() == 10));
            EnqueueCallback(() => Assert.IsTrue(classes.Any(r => r.Name == "Warrior" || r.Name == "Death Knight")));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void Get_Guild_Rewards_Data()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            IEnumerable<GuildRewardInfo> rewards = null;
            WowExplorer.GetGuildRewards(cb =>
            {
                rewards = cb;
                done = true;
            });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsTrue(rewards.Count() == 42));
            EnqueueCallback(() => Assert.IsTrue(rewards.Any(r => r.Achievement != null)));
            EnqueueTestComplete();
        }


        [TestMethod]
        [Asynchronous]
        public void Get_Guild_Perks_Data()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            IEnumerable<GuildPerkInfo> perks = null;
            WowExplorer.GetGuildPerks(cb =>
            {
                perks = cb;
                done = true;
            });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.IsTrue(perks.Count() == 24));
            EnqueueCallback(() => Assert.IsTrue(perks.Any(r => r.Spell != null)));
            EnqueueTestComplete();
        }

        //Update the file path to the Data folder in the Explorers.Test project
        [TestMethod]
        [Asynchronous]
        public void Get_Realms_From_Json_File()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            IEnumerable<Realm> realms3 = null;
            WowExplorer.GetRealms(realms1 =>
                {
                    var data = Application.GetResourceStream(new Uri("Data/jsonRealmsFile.txt", UriKind.Relative)).Stream;
                    IEnumerable<Realm> realms2 =
                        JsonUtility.FromJSONStream<RealmsData>(new StreamReader(data)).Realms;

                    realms3 = realms1.Intersect(realms2);
                    done = true;
                });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.AreEqual(0, realms3.Count()));
            EnqueueTestComplete();
        }


        //Update the file path to the Data folder in the Explorers.Test project
        [TestMethod]
        [Asynchronous]
        public void Get_Character_From_Json_File()
        {
            bool done = false;
            var WowExplorer = new WowExplorer(Region.US);

            Character briandek = null;
            Character briandekFromJsonFile = null;
            WowExplorer.GetCharacter("skullcrusher", "briandek", CharacterOptions.GetEverything, cb =>
                    {
                        briandek = cb;
                        var data = Application.GetResourceStream(new Uri("Data/jsonCharacterFile.txt", UriKind.Relative)).Stream;
                        briandekFromJsonFile = JsonUtility.FromJSONStream<Character>(new StreamReader(data));
                        done = true;
                    });

            EnqueueConditional(() => done);
            EnqueueCallback(() => Assert.AreEqual(0, briandek.CompareTo(briandekFromJsonFile)));
            EnqueueTestComplete();
        }
    }
}